package mmi.utils;

import java.sql.Timestamp;
import java.util.Calendar;

import mmi.gui.shape.Shape;

/**
 * Action is some sort of wrapper to actions. This wrapper can be used to link actions to shapes
 * and events, and give them a timestamp. It makes sure that shapes and such don't get loaded with
 * irrelevant information, as a part of seperation of concerns.
 * 
 * Actions may or may not have been based on shape-events.
 * 
 * @author louis
 *
 */
public class Action {

	/*
	 * 
	 */
	public static enum Actions {
		AddObject,
		DeleteObject,
		FillObject,
		FillContourObject,
		TopObject,
		ResizeObject,
		MoveObject,
		RotateObject,

		MousePress,
		MouseRelease,
		MouseClick,

		StartApplication,
	}

	/**
	 * 
	 * @param actionName
	 * @return
	 */
	public static Actions getAction(String actionName)
	{
		Actions a = null;

		if(actionName.equals("AddObject"))
		{
			a = Actions.AddObject;
		} else if(actionName.equals("DeleteObject"))
		{
			a = Actions.DeleteObject;
		} else if(actionName.equals("FillObject"))
		{
			a = Actions.FillObject;
		} else if(actionName.equals("TopObject"))
		{
			a = Actions.TopObject;
		} else if(actionName.equals("ResizeObject"))
		{
			a = Actions.ResizeObject;
		} else if(actionName.equals("RotateObject"))
		{
			a = Actions.RotateObject;
		} else if(actionName.equals("MoveObject"))
		{
			a = Actions.MoveObject;
		} else if(actionName.equals("MousePress"))
		{
			a = Actions.MousePress;
		} else if(actionName.equals("MouseRelease"))
		{
			a = Actions.MouseRelease;
		} else if(actionName.equals("MouseClick"))
		{
			a = Actions.MouseClick;
		} else if(actionName.equals("StartApplication"))
		{
			a = Actions.StartApplication;
		}

		return a;
	}

	/**
	 * 
	 * @param a
	 * @return
	 */
	public static boolean saveWorthyAction(Actions a)
	{
		switch (a) {
		case AddObject:
			return true;
		case ResizeObject:
			return true;
                case RotateObject:
                        return true;
		case MoveObject:
			return true;
		case DeleteObject:
			return false;
		default:
			return false;
		}
	}

	private Actions action = null;

	private Timestamp timeStart = null;
	private Timestamp timeEnd = null;

	private Shape shape = null;

	/**
	 * 
	 * @param action
	 */
	public Action(Actions action) {
		this.action = action;
		this.timeStart = new java.sql.Timestamp(Calendar.getInstance().getTime().getTime());
		this.timeEnd = new java.sql.Timestamp(Calendar.getInstance().getTime().getTime());
		this.shape = null;
	}

	/**
	 * 
	 * @param action
	 * @param shape
	 */
	public Action(Actions action, Shape shape) {
		this.action = action;
		this.timeStart = new java.sql.Timestamp(Calendar.getInstance().getTime().getTime());
		this.timeEnd = new java.sql.Timestamp(Calendar.getInstance().getTime().getTime());
		this.shape = shape;
	}

	/**
	 * 
	 * @param action
	 * @param shape
	 * @param time
	 */
	public Action(Actions action, Shape shape, Timestamp time) {
		this.action = action;
		this.timeEnd = time;
		this.shape = shape;
	}

	public Action(Actions action, Shape shape, Timestamp timeStart, Timestamp timeEnd) {
		this.action = action;
		this.timeStart = timeStart;
		this.timeEnd = timeEnd;
		this.shape = shape;
	}

	/**
	 * @return the action
	 */
	public Actions getAction() {
		return action;
	}

	/**
	 * @return the shape
	 */
	public Shape getShape() {
		return shape;
	}

	/**
	 * @return the time
	 */
	public Timestamp getTimeEnd() {
		return timeEnd;
	}

	public Timestamp getTimeStart()
	{
		return timeStart;
	}

	/**
	 * 
	 * @return
	 */
	public String getXMLRepresentation()
	{
		String s = "";

		s += "\t<Action type=\"" + action + "\">\n";

		if(shape != null) {
			s += shape.getXMLRepresentation() + "\n";
		}

		s += "\t\t" + "<TimestampStart>" + timeStart.toString() + "</TimestampStart>\n";
		s += "\t\t" + "<TimestampEnd>" + timeEnd.toString() + "</TimestampEnd>\n";
		s += "\t</Action>";

		return s;
	}

	/**
	 * @param action the action to set
	 */
	public void setAction(Actions action) {
		this.action = action;
	}

	/**
	 * @param shape the shape to set
	 */
	public void setShape(Shape shape) {
		this.shape = shape;
	}
	/**
	 * @param time the time to set
	 */
	public void setTime(Timestamp time) {
		this.timeEnd = time;
	}
	/**
	 * 
	 */
	@Override
	public String toString()
	{
		String s = "";

		if(action != null) {
			s += "Action Type: " + action + "\t|\t";
		}

		if(timeEnd != null) {
			s += "(" + timeEnd.toString() + ")\t|\t";
		}

		if(shape != null) {
			s += shape.toString();
		}

		return s;
	}

}
